from flask import Blueprint, request, render_template, redirect, session, flash, url_for, g, jsonify
from app.models import CommentModel
from ..decorators import login_required
from ..exts import db

bp=Blueprint('comment',__name__,url_prefix='/comment')

@bp.route('/commentManage')
def comment_manage():
    comments=CommentModel.query.filter_by(is_deleted=False).order_by(CommentModel.create_time.desc()).all()
    return render_template('commentmanage.html',comments=comments)



@bp.route('/searchComment', methods=['GET', 'POST'])
def search_comment():
    if request.method == 'POST':
        nickname = request.form.get('nickname', '').strip()
        content = request.form.get('content', '').strip()
        search = request.form.get('search')
        if search == 'manage':
            querycomment = CommentModel.query
            if nickname:
                querycomment = querycomment.filter(CommentModel.nickname.ilike(f'%{nickname}%'))
            if content:
                querycomment = querycomment.filter(CommentModel.content.ilike(f'%{content}%'))
            comments = querycomment.filter_by(is_deleted=False).order_by(CommentModel.create_time.desc()).all()
            return render_template('commentmanage.html', comments=comments)
        elif search == 'recyler':
            querycomment = CommentModel.query
            if nickname:
                querycomment = querycomment.filter(CommentModel.nickname.ilike(f'%{nickname}%'))
            if content:
                querycomment = querycomment.filter(CommentModel.content.ilike(f'%{content}%'))
            comments = querycomment.filter_by(is_deleted=True).order_by(CommentModel.create_time.desc()).all()
            return render_template('commentrecyler.html', comments=comments)


@bp.route('/editComment/<int:comment_id>', methods=['GET', 'POST'])
@login_required
def edit_comment(comment_id):
    comment=CommentModel.query.get_or_404(comment_id)
    nickname=request.form.get('nickname4')
    content=request.form.get('content4')
    comment.nickname=nickname
    comment.content=content
    db.session.commit()
    # return jsonify({'code':0,'message':'修改成功'})
    return redirect(url_for('comment.comment_manage'))

@bp.route('/deleteComment/<int:comment_id>')
def delete_comment(comment_id):
    comment=CommentModel.query.get_or_404(comment_id)
    # comment = CommentModel.query.filter_by(id=comment_id).first()
    comment.is_deleted=True
    db.session.commit()
    flash('该留言已经移入回收站', 'info')
    return redirect(url_for('comment.comment_manage'))


@bp.route('/recoveryComment/<int:comment_id>')
def recovery_comment(comment_id):
    comment=CommentModel.query.get_or_404(comment_id)
    comment.is_deleted=False
    db.session.commit()
    flash('该留言已经复原', 'info')
    return redirect(url_for('comment.comment_recyler'))


@bp.route('/deleteCommentPermanently/<int:comment_id>')
def delete_comment_permanently(comment_id):
    comment=CommentModel.query.get_or_404(comment_id)
    # if g.user.id==user.id:
    db.session.delete(comment)
    db.session.commit()
    flash('该留言已永久删除！！！', 'danger')
    return  redirect(url_for('comment.comment_recyler'))

@bp.route('/commentRecyler')
def comment_recyler():
    comments=CommentModel.query.filter_by(is_deleted=True).order_by(CommentModel.create_time.desc()).all()
    return render_template('commentrecyler.html',comments=comments)


